package com.elvin.httptask;

import org.apache.http.Header;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class AtyAdvanceAsyncHttp extends Activity implements OnClickListener {
	
	private Button btnAdvanceGet;
	private Button btnAdvancePost;
	
	private EditText etUserName3;
	private EditText etPassword3;
	
	private EditText etUserName4;
	private EditText etPassword4;
	
	private TextView tvResult3;
	private TextView tvResult4;
	
	private AsyncHttpClient client = new AsyncHttpClient();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_advanceasynchttp);
		
		btnAdvanceGet = (Button) findViewById(R.id.btnAdvanceGet);
		btnAdvancePost = (Button) findViewById(R.id.btnAdvancePost);
		
		etUserName3 = (EditText) findViewById(R.id.etUserName3);
		etPassword3 = (EditText) findViewById(R.id.etPassword3);
		
		etUserName4 = (EditText) findViewById(R.id.etUserName4);
		etPassword4 = (EditText) findViewById(R.id.etPassword4);
		
		tvResult3 = (TextView) findViewById(R.id.tvResult3);
		tvResult4 = (TextView) findViewById(R.id.tvResult4);
		
		btnAdvanceGet.setOnClickListener(this);
		btnAdvancePost.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		
			case R.id.btnAdvanceGet:{
				
					String sendData = "?";
					sendData += "username="+etUserName3.getText().toString().trim();
					sendData += "&password="+etPassword3.getText().toString().trim();
					
					client.get(CommonURL.resGETSUBMIT_URL+sendData, new AsyncHttpResponseHandler() {
					
						@Override
						public void onSuccess(int statusCode, Header[] headers, byte[] response) {
							// TODO Auto-generated method stub
							tvResult3.setText(new String(response));
							System.out.println(statusCode);
							System.out.println(headers);
						}
					
					@Override
					public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable e) {
						// TODO Auto-generated method stub
						
					}
				});
				break;
			}	
	
			case R.id.btnAdvancePost:{
				
				 RequestParams params = new RequestParams();
				 params.put("username", etUserName4.getText().toString().trim());
				 params.put("password", etPassword4.getText().toString().trim());
			        
				client.post(CommonURL.resPOSTSUBMIT_URL, params, new AsyncHttpResponseHandler() {
					
					@Override
					public void onSuccess(int statusCode, Header[] headers, byte[] response) {
						// TODO Auto-generated method stub
						tvResult4.setText(new String(response));
					}
					
					@Override
					public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable e) {
						// TODO Auto-generated method stub
						
					}
				});
				
				break;
			}
			
			
			
		}
	}

}
